package com.xayy.health.ds.controller;

import cn.kawins.base.result.ResultBody;
import com.xayy.health.ds.service.DsTaskChildInfoService;
import com.xayy.health.mgt.model.TaskInfo;
import com.xtck.ha.common.core.constant.HttpStatus;
import com.xtck.ha.common.core.utils.poi.ExcelUtil;
import com.xtck.ha.common.core.web.controller.BaseController;
import com.xtck.ha.common.core.web.domain.AjaxResult;
import com.xtck.ha.common.core.web.page.TableDataInfo;
import com.xayy.health.ds.model.DsTaskInfo;
import com.xayy.health.ds.service.DsTaskInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 模型列表 controller
 *
 * @ClassName: DsTaskInfoController
 * @Description: 外部访问接口
 * @author liuyuan
 * @date 2025-07-08
 */
@Api(tags = "任务信息表管理")
@RestController
@RequestMapping("/health-mgr-back/v1.0/task")
public class DsTaskInfoController extends BaseController {

    @Autowired
    private DsTaskInfoService dsTaskInfoService;
    @Autowired
    private DsTaskChildInfoService dsTaskChildInfoService;


    @ApiOperation("分页查询任务信息表列表")
    @GetMapping("/taskPageList")
    public AjaxResult page(DsTaskInfo dsTaskInfo)
    {
        startPage();
        List<DsTaskInfo> list = dsTaskInfoService.selectDsTaskInfoList(dsTaskInfo);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setList(list);
        rspData.setMsg("查询成功");
        rspData.setTotal(dsTaskInfoService.count(dsTaskInfo));
        return AjaxResult.success(rspData);
    }

    @ApiOperation("列表查询任务信息表")
    @GetMapping("/list")
    public List<DsTaskInfo> list(DsTaskInfo dsTaskInfo)
    {
        List<DsTaskInfo> list = dsTaskInfoService.selectTList(dsTaskInfo);
        return list;
    }

    @ApiOperation("导出任务信息表列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, DsTaskInfo dsTaskInfo)
    {
        List<DsTaskInfo> list = dsTaskInfoService.selectTList(dsTaskInfo);
        ExcelUtil<DsTaskInfo> util = new ExcelUtil<>(DsTaskInfo.class);
        util.exportExcel(response, list, "样例数据");
    }

    @ApiOperation("获取$任务信息表详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(dsTaskInfoService.selectTById(id));
    }

    @ApiOperation("新增$任务信息表")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody DsTaskInfo dsTaskInfo)
    {
        return toAjax(dsTaskInfoService.insertT(dsTaskInfo));
    }

    @ApiOperation("修改任务信息表")
    @PutMapping
    public AjaxResult update(@RequestBody DsTaskInfo dsTaskInfo)
    {
        int i = dsTaskInfoService.updateT(dsTaskInfo);
        return toAjax(i);
    }

    @ApiOperation("删除任务信息表")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(dsTaskInfoService.deleteTByIds(ids));
    }


    /**
     * 根据任务id查询任务数据
     *
     * @param taskId 任务id
     * @return ResultBody
     */
    @ApiOperation(value = "根据任务id查询任务数据")
    @GetMapping("selectTaskInfo/{taskId}")
    public ResultBody selectTaskInfo(@PathVariable(value = "taskId") @ApiParam("任务id") Long taskId) {
        return ResultBody.success(dsTaskInfoService.selectTaskInfo(taskId));
    }

    /**
     * 启动/停止任务状态
     *
     * @param taskInfo 任务对象
     * @return ResultBody
     */
    @ApiOperation(value = "启动/停止任务状态")
    @PostMapping("stopChildThread")
    public ResultBody stopChildThread(@RequestBody DsTaskInfo taskInfo) {
        return ResultBody.success(dsTaskChildInfoService.updateStatusData(taskInfo));
    }


}
